﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Saturn.App.Common;
using Saturn.App.FormOper;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Data;

namespace Saturn.App
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FormLogin : Form
    {
        SaturnWebReference.SaturnWebService soap = new SaturnWebReference.SaturnWebService();
        /// <summary>
        /// 是否在线操作
        /// </summary>
        public Action<bool> CallBack;

        /// <summary>
        /// 窗体辅助类
        /// </summary>
        private FrmLoginOper loginOper = new FrmLoginOper();

        /// <summary>
        /// 
        /// </summary>
        public FormLogin()
        {
            InitializeComponent();

            try
            {
                this.Text = "登录 " + Assembly.GetExecutingAssembly().FullName.Split(',')[1].Split('=')[1];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #region ****---- 控件统一控制可用状态 -----****
        /// <summary>
        /// 控件统一控制可用状态
        /// </summary>
        /// <param name="check"></param>
        private void ControlCheck(bool check)
        {
            cbName.Enabled = check;
            txtPassWord.Enabled = check;
            btnLogin.Enabled = check;
            btnClose.Enabled = check;
        }
        #endregion

        /// <summary>
        /// 查看分辨率
        /// </summary>
        /// <param name="nIndex"></param>
        /// <returns></returns>
        [DllImport("Coredll.dll")]
        public static extern int GetSystemMetrics(int nIndex);


        #region ****----  窗体加载  ----****
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmLogin_Load(object sender, EventArgs e)
        {
            //测试用
            this.cbName.Text = "scada";
            this.txtPassWord.Text = "123";

            //int swidth = GetSystemMetrics(0);
            //int sheight = GetSystemMetrics(1);
            //MessageBox.Show("w:" + swidth + " " + "h:" + sheight);          

            cbName.Focus();

            //统一控件控制
            ControlCheck(true);

            //读取XML
            List<string> lists = loginOper.CheckXml();
            if (lists == null)
            {
                return;
            }


            //下拉绑定
            foreach (var item in lists)
            {
                cbName.Items.Add(item);
            }

            this.cbName.SelectedIndex = lists.Count - 1;


        }
        #endregion





        #region ****---  退出 2014年3月12日9:57:41  -----****
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();

            //GetScanData.RemoveScan();

            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }
        #endregion


        #region ****---- 登录 2014年3月12日 ----****
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(cbName.Text.Trim()) || string.IsNullOrEmpty(txtPassWord.Text))
            {
                CommonSound.MessageBeep(CommonSound.Information);
                MessageBox.Show("请输入账户密码！");
                return;
            }
            #region 登录
            try
            {
                string userinfo = soap.UserLogin(cbName.Text.Trim(), txtPassWord.Text.Trim().ToLower());
                //MessageBox.Show(userinfo);
                userinfo = userinfo.Replace("{", "").Replace("}", "").Replace("=", "").Replace("result", "").Replace("message", "").Replace("result", "").Replace("userId", "").Replace("userName", "").Replace("orgId", "").Replace("orgName", "").Trim();
                string[] userlist = userinfo.Split(',');

                //统一控件控制
                ControlCheck(false);
                //还原控件设置
                ControlCheck(true);
                //if (result[0]=="1")
                if (userlist[0] == "1")
                {
                    //勾选记住用户名
                    if (ckbRemarkName.Checked)
                    {
                        //写入xml文件
                        loginOper.InsertXml(cbName.Text);
                    }
                    else
                    {
                        //写入xml文件
                        loginOper.DeleXml(cbName.Text);
                    }
                    //1,   成功,   35024ECE0B1444EFB076C8AC693D6BFC,   宗冠宇,   YHJL,   益海嘉里
                    CommonCfg.UserId = userlist[2];
                    CommonCfg.Userrealname = userlist[3];
                    CommonCfg.orgId = userlist[4];
                    CommonCfg.orgname = userlist[5];
                    CommonCfg.UserName = cbName.Text;
                    UserLogin.loginName = cbName.Text;

                    CallBack.Invoke(true);
                    Close();
                }
                else
                {
                    CommonSound.MessageBeep(CommonSound.Information);
                    txtPassWord.Text = string.Empty;
                    MessageBox.Show(userlist[1]);
                }
            }
            catch (Exception ex)
            {
                ControlCheck(true);
                MessageBox.Show("发生网络故障，请检查网络是否连接！");
                return;
            }
            #endregion
        }
        #endregion



        #region ****----  控件值改变  ----****
        /// <summary>
        /// 控件值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(cbName.SelectedItem.ToString()))
                txtPassWord.Focus();
        }
        #endregion

        private void txtPassWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (char)13)
            {
                btnLogin_Click(this.btnLogin, new EventArgs());
            }
        }

        private void FrmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnLogin_Click(this.btnLogin, new EventArgs());
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            CommonSound.SipShowIM(CommonSound.SIPF_ON);
        }

    }
}